/**
 * 
 */
package com.training.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.demo2do.core.service.impl.GenericServiceImpl;
import com.training.admin.service.DataService;

/**
 * @author Wilson
 * 
 * @param <T>
 */


@Service("dataService")
public class DataServiceImpl<T> extends GenericServiceImpl<T> implements DataService<T> {
    
    /* (non-Javadoc)
     * @see co.fitstart.admin.service.DataService#count(java.lang.Class)
     */
    public int count(Class<T> clazz) {
        return genericDaoSupport.searchForInt("SELECT COUNT(*) FROM " + ClassUtils.getShortClassName(clazz) + " WHERE disabled = false");
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.DataService#count(java.lang.Class, com.demo2do.core.persistence.support.QueryFilter)
     */
    public int count(Class<T> clazz, QueryFilter queryFilter) {
        return genericDaoSupport.searchForInt("SELECT COUNT(*) FROM " + ClassUtils.getShortClassName(clazz) + " WHERE disabled = false" + queryFilter.clauseSentence(), queryFilter.parameters());
    }
    
    /* (non-Javadoc)
     * @see co.fitstart.admin.service.DataService#listAll(java.lang.Class)
     */
    @SuppressWarnings("unchecked")
    public List<T> listAll(Class<T> clazz) {
        return genericDaoSupport.searchForList("FROM " + ClassUtils.getShortClassName(clazz) + " WHERE disabled = false");
    }

    /* (non-Javadoc)
     * @see co.fitstart.admin.service.DataService#listByPage(java.lang.Class, com.demo2do.core.web.resolver.Page)
     */
    @SuppressWarnings("unchecked")
    public List<T> listByPage(Class<T> clazz, Page page) {
        List<T> result = genericDaoSupport.searchForList("FROM " + ClassUtils.getShortClassName(clazz) + " WHERE disabled = false", page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.DataService#listByPage(java.lang.Class, com.demo2do.core.persistence.support.QueryOrder, com.demo2do.core.entity.page.Page)
     */
    @SuppressWarnings("unchecked")
    public List<T> listByPage(Class<T> clazz, QueryOrder queryOrder, Page page) {
        List<T> result = genericDaoSupport.searchForList("FROM " + ClassUtils.getShortClassName(clazz) + " WHERE disabled = false" + queryOrder.sentence(), page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.DataService#listByPage(java.lang.Class, com.demo2do.core.persistence.support.QueryFilter, com.demo2do.core.persistence.support.QueryOrder, com.demo2do.core.entity.page.Page)
     */
    @SuppressWarnings("unchecked")
	public List<T> listByPage(Class<T> clazz, QueryFilter queryFilter, QueryOrder queryOrder, Page page) {
    	String sentence = "FROM " + ClassUtils.getShortClassName(clazz) + " WHERE disabled = false" + queryFilter.clauseSentence() + queryOrder.sentence();
        Map<String, Object> parameters = queryFilter.parameters();
    	List<T> result = genericDaoSupport.searchForList(sentence, parameters, page.getBeginIndex(), page.getEveryPage());
    	page.calculate();
    	return result;
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.DataService#load(java.lang.Class, java.lang.String)
     */
    public T load(Class<T> clazz, String code, String query) {
        query = StringUtils.isNoneBlank(query) ? query : "code = :code";
        return genericDaoSupport.searchForObject("FROM " + ClassUtils.getShortClassName(clazz) + " WHERE disabled = false AND " + query, "code", code, clazz);
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.DataService#load(java.lang.Class, java.lang.String, java.util.List)
     */
    public T load(Class<T> clazz, String code, List<Long> orgIds, String query) {
        query = StringUtils.isNoneBlank(query) ? query : "code = :code";
        String sentence = "FROM " + ClassUtils.getShortClassName(clazz) + " WHERE disabled = false AND " +query+ " AND organization.id IN (:orgIds)";
        Map<String, Object> params = new HashMap<String, Object>(4);
        params.put("code", code);
        params.put("orgIds", orgIds);
        return genericDaoSupport.searchForObject(sentence, params, clazz);
    }
    
    /* (non-Javadoc)
     * @see co.fitstart.admin.service.DataService#search(java.lang.Class, com.demo2do.core.persistence.support.QueryBuilder, com.demo2do.core.web.resolver.Page)
     */
    @SuppressWarnings("unchecked")
    public List<T> search(Class<T> clazz, QueryBuilder queryBuilder, Page page) {
    	String sentence = queryBuilder.buildSentenceTemplate();
    	Map<String, Object> params = queryBuilder.buildParameters();
        List<T> result = genericDaoSupport.searchForList(sentence, params, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
    }

}
